package com.hae.shop.micoshoppayservice.plugins.strategy.impl;

import com.hae.shop.micoshoppayservice.plugins.PayStrategy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 支付策略工厂
 */
public class StrategyFactory {

    /**
     * 支付策略缓存
     */
    private static Map<String, PayStrategy> strategyBean = new ConcurrentHashMap<>();

    /**
     * 根据payStrategyUrl产生对应的支付策略对象
     * @param payStrategyUrl
     * @return
     */
    public static PayStrategy getPayStrategy(String payStrategyUrl) {
        PayStrategy payStrategy = strategyBean.get(payStrategyUrl);
        if (null != payStrategy) {
            return payStrategy;
        }
        try {
            Class clazz = Class.forName(payStrategyUrl);
            payStrategy = (PayStrategy) clazz.newInstance();
            strategyBean.put(payStrategyUrl, payStrategy);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return payStrategy;
    }
}
